<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ProModel extends Model
{
    //
    public static function get_item()
    {
        $lists = DB::table('pro_item')->orderBy('sort', 'asc')->get();
        return $lists;
    }

    //
    public static function get_alone($id = null)
    {
        if ($id !== null) {
            $array = DB::table('pro_alone')->where('id', $id)->get();
            return $array;
        } else {
            $lists = DB::table('pro_alone')->orderBy('created_at', 'desc')->get();
            return $lists;
        }
    }

    //
    public static function get_item_alone($id = null)
    {
        $id = (int)$id;
        if ($id !== 0) {
            $json = DB::table('pro_alone')->where('item_id', $id)->orderBy('created_at', 'desc')->get()->toJson();
            return $json;
        } else {
            $json = DB::table('pro_alone')->orderBy('created_at', 'desc')->get()->toJson();
            return $json;
        }
    }

    //
    public static function get_img($id = null)
    {
        if ($id !== null) {
            $json = DB::table('pro_img')->where('alone_id', $id)->get();
            return $json;
        } else {
            $lists = DB::table('pro_img')->get();
            return $lists;
        }
    }

    //
    public static function delete_id($id)
    {
        if (DB::table('pro_alone')->where('id', $id)->exists()) {
            DB::table('pro_alone')->where('id', $id)->delete();
            DB::table('pro_img')->where('alone_id', $id)->delete();
            return true;
        } else {
            return false;
        }
    }

    public static function update_pro($id, $array)
    {
        if (DB::table('pro_alone')->where('id', $id)->exists()) {
            DB::table('pro_alone')->where('id', $id)->update($array);
            return true;
        } else {
            return false;
        }
    }

    public static function insert_pro($id, $array)
    {
        $array['id'] = $id;
        $array['created_id'] = Auth::id();
        $array['created_at'] = date('Y-m-d h:i:s');
        if (DB::table('pro_alone')->insert($array))
            return true;
        else
            return false;
    }
}
